package com.br.alex.view.bd;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.br.alex.view.model.Places;

public class FavoritosDB {
private ProjetoCesarDbHelper dbHelper;

public FavoritosDB(ProjetoCesarDbHelper dbHelper) {
		super();
		this.dbHelper = dbHelper;
	}

   private ContentValues getValues(Places sg)
   {
	//"nome TEXT NOT NULL, foto TEXT, especialidade TEXT, resumo TEXT)");

	   ContentValues cv = new ContentValues();
		cv.put("nome", sg.getNome());
		cv.put("icon", sg.getIcon());
		cv.put("tipo", sg.getTipo());
		cv.put("snippet", sg.getSnippet());
		cv.put("coordinates", sg.getCoodinates());
		
	    return cv;
	   
   }
   
   
   public void deletePlaces(){
	   SQLiteDatabase db = dbHelper.getWritableDatabase();
	   db.execSQL("DROP TABLE IF EXISTS cache");
	   Log.i("Deletado","Deletado");
	   db.close();
	   
   }
   
   public void inserir(Places sg){
	   SQLiteDatabase db = dbHelper.getWritableDatabase();
	   
	   db.insert("cache",null, getValues(sg) );
	   
	   db.close();
	  // Log.d("CADASTRADO:", "CADASTRADO:");
	   
   }
   
	public List<Places> listar(){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor c = db.rawQuery("SELECT *  FROM cache",null);
		List<Places> rotas = new ArrayList<Places>();
		
		while (c.moveToNext() ){
			rotas.add(
					new Places(
							c.getString(1), 
							c.getString(2),
							c.getString(3),
							c.getString(5),
							c.getString(4)
						));
			//System.out.println("["+c.getString(1)+"]"+"["+c.getString(2)+"]"+"["+c.getString(3)+"]"+"["+c.getString(4)+"]"+"["+c.getString(5)+"]");
			//System.out.println("["+c.getString(1)+"]"+"["+c.getString(2)+"]");
		}
		System.out.println(rotas.size());
		
		c.close();

		db.close();

		return rotas;
	}
   
}
